package CGW.controller;

import CGW.bean.Dinner_table;
import CGW.bean.Page;
import CGW.dao.BaseDao;
import CGW.dao.Dinner_tableDao;
import CGW.service.Dinner_tableService;
import CGW.utils.ZHI;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/dinner_table")
public class Dinner_tableServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Dinner_tableService service = new Dinner_tableService(new Dinner_tableDao());
        String action = req.getParameter("action");
        if (action == null){
            String page = req.getParameter("page");
            int pageInt = 1;
            String pageSize = req.getParameter("pageSize");
            int pageSizeInt = 100;
            if(page != null){
                pageInt = Integer.parseInt(page);
            }
            if (pageSize != null){
                pageSizeInt = Integer.parseInt(pageSize);
            }

            Page<Dinner_table> pageBean1= service.queryAll(pageInt, pageSizeInt);
            System.out.println(pageBean1.getDatas());
            req.setAttribute("pageBean1",pageBean1);
            req.getRequestDispatcher("dinner_table.jsp").forward(req,resp);

        }else if(action.equals("update")){
            String table_id = req.getParameter("table_id");
            // 依据user_id 获取用户的信息
            Dinner_table dinner_table = service.get(Integer.parseInt(table_id));
            req.setAttribute("dinner_table", dinner_table);
            req.getRequestDispatcher("dinner_table_update.jsp").forward(req, resp);
        }else if(action.equals("update1")){
            String table_id = req.getParameter("table_id");
            String table_status = req.getParameter("table_status");
            if (table_status.equals("1")){
                Dinner_table dinner_table = new Dinner_table(ZHI.zh(table_id),ZHI.zh(table_status));
                BaseDao baseDao = new BaseDao();
                int ret = baseDao.update_one(dinner_table);
                baseDao.commit();
                req.setAttribute("dinner_table", dinner_table);
                resp.sendRedirect("/dinner_table");
            }
            if (table_status.equals("0")){
                Dinner_table dinner_table = new Dinner_table(ZHI.zh(table_id),ZHI.zh(table_status));
                BaseDao baseDao = new BaseDao();
                int ret = baseDao.update_one(dinner_table);
                baseDao.commit();
                req.setAttribute("dinner_table", dinner_table);
                resp.sendRedirect("/dinner_table");
            }
        }else if(action.equals("delete")){
            String table_id = req.getParameter("table_id");
            boolean ret = service.delete(Integer.parseInt(table_id));
            if(ret){
                req.getSession().setAttribute("delMsg", "删除成功");
            }else{
                req.getSession().setAttribute("delMsg", "删除失败");
            }
            resp.sendRedirect("dinner_table");
        }else if(action.equals("add")){
            service.close();
            Dinner_table dinner_table=new Dinner_table();
            req.setAttribute("dinner_table", dinner_table);
            req.getRequestDispatcher("dinner_table_insert.jsp").forward(req, resp);
        }else if(action.equals("delMsg")){
            // 异步请求及响应
            req.getSession().removeAttribute("delMsg");
            resp.getWriter().println("ok");
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Dinner_tableService service = new Dinner_tableService(new Dinner_tableDao());
        String table_id = req.getParameter("table_id");
        String table_name = req.getParameter("table_name");
        String table_status = req.getParameter("table_status");
        String reservation_time = req.getParameter("reservation_time");
        boolean ret = false;
        Dinner_table dinner_table = null;
        boolean isAdd = false;
        if(table_id.equals("")){
            // 新增
            dinner_table = new Dinner_table(table_name,ZHI.zh(table_status),ZHI.dt(reservation_time));
            ret = service.insert(dinner_table);
            isAdd = true;
        }else{
            dinner_table = new Dinner_table(ZHI.zh(table_id),table_name,ZHI.zh(table_status),ZHI.dt(reservation_time));
            System.out.println(dinner_table);
            ret = service.update(dinner_table);
            System.out.println(ret);
        }

        if(ret){
            resp.sendRedirect("dinner_table");

        }else{
            req.setAttribute("dinner_table", dinner_table);
            req.setAttribute("error", table_id.equals("")?"新增失败":"更新失败");
            req.getRequestDispatcher(isAdd?"dinner_table_insert.jsp":"dinner_table_update.jsp").forward(req, resp);
        }


    }
}
